<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initAutoload()
    {
        header('Content-Type:   text/html;   charset=utf-8');
        
         // Ensure front controller instance is present
        $this->bootstrap('frontController');
        
        // Get frontController resource
        $this->_front = $this->getResource('frontController');

        // Add autoloader empty namespace
        $autoLoader =  new Zend_Loader_Autoloader_Resource(array(
            'basePath'      => APPLICATION_PATH,
            'namespace'     => '',
            'resourceTypes' => array(
                'model' => array(
                    'path'      => 'models/',
                    'namespace' => 'Model_'
                ),
            ),
        ));
        
        // Return it, so that it can be stored by the bootstrap
        return $autoLoader;
    }
	
    /**
     * Initialize the site configuration
     *
     * @return Zend_Config_Xml
     */
    protected function _initConfig()
    {
        // Retrieve configuration from file
        $config = new Zend_Config_Xml(APPLICATION_PATH . '/data/config.xml', APPLICATION_ENV);

        // Add config to the registry so it is available sitewide
        $registry = Zend_Registry::getInstance();
        $registry->set('config', $config);
        // Return it, so that it can be stored by the bootstrap
        return $config;
    }
   
    
	/**
     * Initialize the view
     *
     * @return Zend_View
     */
    protected function _initView()
    {
    	#===============================================================
		$view = new Zend_View(); 
		Zend_Registry::set('view', $view);      
		
		date_default_timezone_set('Asia/Shanghai');
		
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');   
		$viewRenderer ->setView($view)  
	                         ->setViewSuffix('html'); 
	                         
      	// base helpers
        $view->addHelperPath('Bcms/View/Helper', 'Bcms_View_Helper');
//        $view->addHelperPath('Bcms/Content/Control', 'Bcms_Content_Control');
        
        $helperDirs = Bcms_Filesystem_Dir::getDirectories(BASE_PATH . '/library/Bcms/View/Helper');
        if (is_array($helperDirs)) {
            foreach ($helperDirs as $dir) {
                $view->addHelperPath(BASE_PATH . '/library/Bcms/View/Helper/' . $dir, 'Bcms_View_Helper_' . ucfirst($dir));
            }
        }
        
		Zend_Registry::set('viewRenderer', $viewRenderer);    
		return $view;
   }
   

     /**
     * Initialize data base
     *
     * @return Zend_Db_Adapter_...
     */
    protected function _initDb()
    {
        $this->bootstrap('config');
        // Get config resource
        $config = $this->getResource('config');

        // Setup database
        $db = Zend_Db::factory($config->database->adapter, $config->database->toArray());
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        $db->query("SET NAMES 'utf8'");
        $db->query("SET CHARACTER SET 'utf8'");
        Zend_Db_Table::setDefaultAdapter($db);
        // Return it, so that it can be stored by the bootstrap
        return $db;
    }
    
    
     /**
     * Initialize the controllers
     *
     * @return void
     */
   protected function _initControllers() 
   {
//     	var_export($this);exit();
         $this->_front->setControllerDirectory(array(
          	'default' => APPLICATION_PATH . '/module/public/controllers',
		    'admin' => APPLICATION_PATH . '/admin/controllers',
	   ));
	   
     } 
   
	

}

